//
//  MainViewController.swift
//  MicroBlog
//
//  Created by Gao on 16/3/1.
//  Copyright © 2016年 Gao. All rights reserved.
//


import UIKit
/// 主控制器
class MainViewController: UITabBarController
{
    /// 加号按钮
    lazy var composeButton:UIButton =
    {
        let button = UIButton()
        //前景图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        //背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        button.addTarget(self, action: Selector("composeButtonClick"), forControlEvents: .TouchUpInside)
        return button
    }()
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        setupController()
    }
    
    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }
    
    override func viewWillAppear(animated: Bool)
    {
        super.viewWillAppear(animated)
        addComposeButton()
    }
    
    /**
     加号按钮
     */
    func addComposeButton()
    {
        tabBar.addSubview(composeButton)
        //1.宽度
        let width = tabBar.bounds.width / CGFloat((self.viewControllers?.count)!)
        //2.位置
        let rect = CGRectMake(0, 0, width, 49)
        //3.位置的修正
        composeButton.frame = CGRectOffset(rect, width * 2, 0)
    }
    
    /**
     子控制器的配置
     */
    func setupController()
    {
        //加载子控制器描述文件
        let jsonFileName = NSBundle.mainBundle().pathForResource("controller.json", ofType: nil)
        if let path = jsonFileName
        {
            let jsonData = NSData(contentsOfFile: path)
            do
            {
                let arr = try NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions.MutableContainers) as! NSArray
                for var i = 0 ; i < arr.count ; ++i
                {
                    let dict = arr[i] as! Dictionary<String,String>
                    addChildViewController(dict["vcName"]!, title: dict["title"]!, imageNamed: dict["imageName"]!)
                }
            }
            catch
            {
                //如果读取失败 直接创建
                print(error)
                addChildViewController("HomeViewController", title: "首页", imageNamed: "tabbar_home")
                addChildViewController("MessageViewController", title: "消息", imageNamed: "tabbar_message_center")
                addChildViewController("NullViewController", title: "", imageNamed: "")
                addChildViewController("DiscoverViewController", title: "发现", imageNamed: "tabbar_discover")
                addChildViewController("MeViewController", title: "我", imageNamed: "tabbar_profile")
            }
        }
        
    }
    
    /**
     添加子控制器
     - parameter childControllerName: 子控制器
     - parameter title:           标题名
     - parameter imageNamed:      图片名
     */
    func addChildViewController(childControllerName: String,title:String,imageNamed:String)
    {
        /*写法一:不考虑以后需要替换标题(例如节假日图标替换等等)
        let nav = UINavigationController(rootViewController: childController)
        childController.title = title
        childController.tabBarItem.image = UIImage(named: imageNamed)
        childController.tabBarItem.selectedImage = UIImage(named: imageNamed + "_highlighted")
        self.addChildViewController(nav)
        */
        
        //1.获取命名空间
        let namespace = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
        //2.获得类名
        let Class = NSClassFromString("\(namespace).\(childControllerName)") as! UIViewController.Type
        //3.生成对象
        let childController = Class.init()
        //子控制器的配置
        let nav = UINavigationController(rootViewController: childController)
        childController.title = title
        if imageNamed != ""
        {
            childController.tabBarItem.image = UIImage(named: imageNamed)
            childController.tabBarItem.selectedImage = UIImage(named: imageNamed + "_highlighted")
        }
        self.addChildViewController(nav)
    }
    /**
     加号按钮点击事件
     */
    func composeButtonClick()
    {
        
    }
    
}
